<!-- 用户列表 -->
<template>
  <div class="user-list">
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item><a href="/">用户管理</a></el-breadcrumb-item>
      <el-breadcrumb-item>用户列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card class="box-card">
      <el-tabs v-model="activeName" @tab-click="changeTabs">
        <el-tab-pane
          v-for="tabListData in tabListDatas"
          :key="tabListData.level"
          :label="tabListData.name"
          :name="tabListData.name"
        ></el-tab-pane>
      </el-tabs>
      <el-table :data="userLists.data" border style="width: 100%">
        <el-table-column fixed prop="id" label="序号"> </el-table-column>
        <el-table-column prop="username" label="姓名"> </el-table-column>
        <el-table-column label="头像" prop="imgsrc">
          <!-- 当条数据的值 -->
          <template slot-scope="scope">
            <img
              v-if="scope.row.imgsrc"
              :src="Setting.baseUrl + scope.row.imgsrc"
              alt=""
              style="width: 50px"
            />
          </template>
        </el-table-column>
        <el-table-column prop="sex" label="性别" width="50"> </el-table-column>
        <el-table-column prop="age" label="年龄" width="50"> </el-table-column>
        <el-table-column prop="phone" label="手机号"> </el-table-column>
        <el-table-column prop="email" label="邮箱"> </el-table-column>
        <el-table-column prop="address" label="地址" width="50">
        </el-table-column>
        <el-table-column prop="level" label="等级" width="50">
        </el-table-column>
        <el-table-column prop="is_show" label="是否可用" width="120">
          <!-- 每一条数据的值可以通过scope拿到 -->
          <!-- 1:可用 -->
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.is_show"
              active-value="1"
              inactive-value="0"
              active-text="可"
              inactive-text="否"
              active-color="#13ce66"
              inactive-color="#ff4949"
            >
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="150">
          <template slot-scope="scope">
            <el-button
              @click="handleDeleteClick(scope.row)"
              type="danger"
              size="small"
              >删除</el-button
            >
            <el-button type="primary" size="small">编辑</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[5, 10, 15]"
        :page-size="5"
        layout="total, sizes, prev, pager, next, jumper"
        :total="userLists.count"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
//例如：import 《组件名称》 from '《组件路径》';
import Setting from "@/utils/setting.js";
import { userDeleApi } from "@/api/user.js";
export default {
  //import引入的组件需要注入到对象中才能使用
  components: {},
  data() {
    //这里存放数据
    return {
      Setting: Setting,
      page: 1,
      size: 5,
      level: 0,
      currentPage: 1,
      tabListDatas: [
        { level: 0, name: "全部" },
        { level: 1, name: "管理员" },
        { level: 2, name: "普通用户" },
        { level: 3, name: "会员" },
      ],
      activeName: "全部",
    };
  },
  //计算属性 类似于data概念
  computed: {
    userLists() {
      console.log(this.$store.state.user.userListDatas);
      return this.$store.state.user.userListDatas;
    },
  },
  //监控data中的数据变化
  watch: {},
  //方法集合
  methods: {
    getUserListDatas() {
      console.log("执行");
      this.$store.dispatch("actChangeUserListDatas", {
        page: this.page,
        size: this.size,
        level: this.level,
      });
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.page = 1;
      this.size = val;
      this.getUserListDatas();
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.page = val;
      this.getUserListDatas();
    },
    changeTabs(data) {
      console.log(data.index);
      console.log(this.tabListDatas[data.index]);
      this.level = this.tabListDatas[data.index].level;
      this.getUserListDatas();
    },
    // 删除事件
    handleDeleteClick(rowData) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          userDeleApi(rowData).then((res) => {
            console.log(res);
            if (res.data.status == 200) {
              this.$message({
                type: "success",
                message: "删除成功!",
              });
              this.getUserListDatas();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
  beforeCreate() {}, //生命周期 - 创建之前
  //生命周期 - 创建完成（可以访问当前this实例）
  created() {},
  beforeMount() {}, //生命周期 - 挂载之前
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {
    this.getUserListDatas();
  },
};
</script>
<style>
</style>